<template>
  <base-layout
    :dialog-title="dialogTitle"
    :dialog-visible="dialogVisible"
    @dialog-close="dialogClose"
  >
    asdasda
    <template #searchPanel>
      <search-panel
        v-slot="{ form }"
        :selectPer="'purchaseWh:page'"
        @search="search"
        :showSearchButton="true"
        :showAddButton="false"
      >
        <el-select
          clearable
          style="width: 200px"
          v-model="form.status"
          placeholder="库存状态"
        >
          <el-option
            v-for="item in getDict(['库存状态'])['库存状态']"
            :key="item.index"
            :value="item.value"
            :label="item.text"
          />
        </el-select>
        <CarModel style="width: 350px" v-model="modelList" ref="carModel" />
        <el-date-picker
          v-model="form.inTime"
          type="datetimerange"
          start-placeholder="创建日期"
          end-placeholder="创建日期"
          clearable
          value-format="yyyy-MM-dd HH:mm:ss"
          :default-time="['00:00:00', '23:59:59']"
        />
        <el-input
          v-model="form.client"
          clearable
          placeholder="请输入配车合同号"
          style="width: 200px"
        />
      </search-panel>
    </template>
    <template #contentPanel>
      <data-table
        :value="tableData"
        :total-count="totalCount"
        :has-action="true"
        :table-column-attributes="tableColumnAttributes"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :width="160"
        @row-dblclick="handleDetails"
      >
        <template #action="{ row }">
          <i
            class="el-icon-picture-outline"
            title="上传PDI"
            style="margin-left: 15px; cursor: pointer"
            @click="uploadPdi(row)"
          ></i>
          <i
            v-if="row.wareHouseStatus !== '已入库'"
            class="el-icon-s-check"
            title="完成入库"
            style="margin-left: 15px; cursor: pointer"
            v-permission="'purchaseWh:okInBound'"
            @click="okInBound(row)"
          ></i>
          <i
           v-if="row.wareHouseStatus !== '已入库'"
            class="el-icon-edit"
            title="修改"
            style="margin-left: 15px; cursor: pointer"
            v-permission="'purchaseWh:del'"
            @click="updateUser(row)"
          ></i>
          <i
            class="el-icon-refresh-right"
            title="退货"
            style="margin-left: 15px; cursor: pointer"
            v-permission="'purchaseWh:saleReturn'"
            @click="returnPurchase(row)"
          ></i>
        </template>
      </data-table>
      <dialog-upload-image
        @pdi-submit="pdiSubmit"
        v-model="uploadPDI.pdi"
        :visible-pdi="visiblePDI"
      />
    </template>
    <template #dialog>
      <purchaseListView
        :dialog-data="dialogData"
        :on-success="okInBoundSuccess"
      />
    </template>
  </base-layout>
</template>
<script>
import baseMixin from '@/mixins/base'
import { CarModel } from '@/components/index'
import purchaseListView from './components/purchaseListView'
import dialogUploadImage from '@/components/DialogUploadImage'
import {
  dataList,
  details,
  saleReturn,
  okInBound,
  uploadPdi
} from '@/api/pre-sale/newCarStock/putInStorageManagement/purchaseInventory'
import { getDict } from '@/utils/index'

export default {
  components: {
    CarModel,
    purchaseListView,
    dialogUploadImage
  },
  mixins: [
    baseMixin({
      idName: 'id',
      findData: dataList,
      findDataById: details,
      dataName: ['content', 'totalRecords'],
      format(res) {
        return res.map((data) => {
          data.brandSeriesModel =
            data.brandName + '/' + data.seriesName + '/' + data.modelName
          return data
        })
      }
    })
  ],
  data() {
    return {
      dialogTitle: '采购入库详情',
      modelList: [],
      searchOptions: { allotType: true },
      getDict: getDict,
      uploadPDI: {
        pdi: '',
        id: ''
      },
      visiblePDI: false,
      form: {
        brandId: '',
        seriesId: '',
        modelId: ''
      },
      tableColumnAttributes: [
        {
          name: '门店',
          value: 'storeName',
          type: 'text'
        },
        {
          name: '配车合同号',
          value: 'contractNum',
          type: 'text'
        },
        {
          name: '入库单号',
          value: 'purchaseWhNum',
          type: 'text'
        },
        {
          name: '仓库',
          value: 'storageName',
          type: 'text'
        },
        {
          name: '仓库位置',
          value: 'storageAddress',
          type: 'text'
        },

        {
          name: '仓位',
          value: 'storageSiteName',
          type: 'text'
        },
        {
          name: '供应商',
          value: 'supplierName',
          type: 'text'
        },
        {
          name: '车架号',
          value: 'vin',
          type: 'text'
        },
        {
          name: '品牌/车系/车型',
          value: 'brandSeriesModel',
          type: 'text'
        },

        {
          name: '标准进价',
          value: 'criterionPrice',
          type: 'text'
        },
        {
          name: '实际进价',
          value: 'realityPrice',
          type: 'text'
        },
        {
          name: '状态',
          value: 'wareHouseStatus',
          type: 'text'
        },
        {
          name: '创建时间',
          value: 'createTime',
          type: 'text'
        },
        {
          name: '创建人',
          value: 'wareHouseUserName',
          type: 'text'
        }
      ],
      dict: {}
    }
  },
  methods: {
    uploadPdi(row) {
      this.uploadPDI.id = row.id
      this.visiblePDI = true
    },
    async pdiSubmit(type) {
      if (type === 'cancel') {
        this.visiblePDI = false
        return
      }
      const { success } = await uploadPdi(this.uploadPDI)
      if (success) {
        this.visiblePDI = false
        this.$message.success('操作成功')
      } else {
        this.$message.error('操作失败')
      }
    },
    okInBound({ id }) {
      this.$confirm('确定要进行入库操作吗, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          await okInBound(id)
          this.setTableData()
           this.$message.success('操作成功')
        })
        .catch(() => {})
    },
    returnPurchase({ id }) {
      this.$prompt('退货问题描述：', '采购退货', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        showInput: true,
        inputPlaceholder: '退货问题描述'
        //  inputPattern: /W+/,
        //  inputErrorMessage: '请输入退货问题描述'
      })
        .then(async({ value }) => {
          const { code } = await saleReturn({ id, remark: value })
          if (code === '200') {
            this.$message.success('操作成功')
          }
          this.setTableData()
        })
        .catch(() => {})
    },
    handleDetails(row) {
      this.handleDataChange(row)
      this.dialogVisible = true
    },
    okInBoundSuccess() {
      this.dialogVisible = false
    },
    // 修改
    updateUser(row) {
      const { id } = row
      this.$router.push({
        name: 'editPurchaseInventory',
        params: { id }
      })
    },
    // 查询
    search(form) {
      if (this.modelList.length > 0) {
        form.brandId = this.modelList[0]
        form.seriesId = this.modelList[1]
        form.modelId = this.modelList[3]
      } else {
        form.brandId = ''
        form.seriesId = ''
        form.modelId = ''
      }
      if (form.inTime && form.inTime.length > 0) {
        form.inBoundMinTime = form.inTime[0]
        form.inBoundMaxTime = form.inTime[1]
      } else {
        form.inBoundMinTime = ''
        form.inBoundMaxTime = ''
      }
      delete form.inTime
      this.searchOptions = { ...form, allotType: true }
      this.currentPage = 1
      this.setTableData()
    }
  }
}
</script>
